<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>驼峰命名转蛇形命名工具</title>
    <link rel="stylesheet" href="https://www.unpkg.com/ew-message/dist/ew-message.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 页面基本样式 */
        :root {
            --primary-color: #2563eb;
            --primary-hover: #1d4ed8;
            --secondary-color: #f3f4f6;
            --text-color: #374151;
            --border-color: #e5e7eb;
            --success-color: #10b981;
            --error-color: #ef4444;
            --radius: 8px;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: #f9fafb;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: var(--text-color);
        }

        .container {
            background-color: #fff;
            padding: 2rem;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
            border-radius: var(--radius);
            width: 100%;
            max-width: 480px;
            border: 1px solid var(--border-color);
        }

        .app-header {
            display: flex;
            align-items: center;
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--border-color);
        }

        .app-icon {
            background: linear-gradient(135deg, var(--primary-color), #4f46e5);
            width: 40px;
            height: 40px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 12px;
            color: white;
            font-size: 20px;
        }

        h1 {
            font-size: 1.5rem;
            font-weight: 600;
            color: #111827;
            margin: 0;
        }

        .input-group {
            margin-bottom: 1.25rem;
        }

        label {
            font-size: 0.875rem;
            font-weight: 500;
            display: block;
            margin-bottom: 0.5rem;
            color: #4b5563;
        }

        input {
            width: 100%;
            padding: 0.75rem 1rem;
            font-size: 0.875rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            background-color: #fff;
            transition: all 0.2s;
            color: var(--text-color);
        }

        input:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        input::placeholder {
            color: #9ca3af;
        }

        .btn-group {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 0.75rem;
            margin-bottom: 1.25rem;
        }

        .btn {
            padding: 0.75rem 1rem;
            font-size: 0.875rem;
            font-weight: 500;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--radius);
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .btn i {
            margin-right: 0.5rem;
        }

        .btn:hover {
            background-color: var(--primary-hover);
        }

        .btn-secondary {
            background-color: var(--secondary-color);
            color: var(--text-color);
        }

        .btn-secondary:hover {
            background-color: #e5e7eb;
        }

        .result {
            margin-top: 0.5rem;
            background-color: #f9fafb;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            padding: 1rem;
        }

        .result-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 0.5rem;
        }

        .result-title {
            font-weight: 500;
            font-size: 0.875rem;
            color: #4b5563;
        }

        #snakeCaseOutput {
            background-color: transparent;
            border: none;
            padding: 0;
            font-family: monospace;
            font-size: 1rem;
            color: #111827;
            width: 100%;
        }

        #snakeCaseOutput:focus {
            outline: none;
            box-shadow: none;
        }

        .alert {
            color: var(--error-color);
            font-size: 0.875rem;
            margin-top: 0.5rem;
            display: flex;
            align-items: center;
        }

        .alert i {
            margin-right: 0.5rem;
        }

        .success-message {
            color: var(--success-color);
            display: flex;
            align-items: center;
            font-size: 0.875rem;
            margin-top: 0.5rem;
        }

        .success-message i {
            margin-right: 0.5rem;
        }

        .tooltip {
            position: relative;
            display: inline-block;
            cursor: pointer;
        }

        .tooltip .tooltiptext {
            visibility: hidden;
            width: 120px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            margin-left: -60px;
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 0.75rem;
        }

        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="app-header">
            <div class="app-icon">
                <i class="fas fa-code"></i>
            </div>
            <h1>驼峰命名转换工具</h1>
        </div>

        <div class="input-group">
            <label for="camelCaseInput">请输入驼峰命名</label>
            <input type="text" id="camelCaseInput" placeholder="例如：camelCaseExample">
        </div>

        <div class="input-group">
            <label for="camelCaseSymbolInput">请输入转换符号</label>
            <input type="text" id="camelCaseSymbolInput" placeholder="例如：-" value="-">
        </div>

        <div class="btn-group">
            <button id="convertBtn" class="btn">
                <i class="fas fa-sync-alt"></i> 转换
            </button>
            <button id="copyBtn" class="btn btn-secondary">
                <i class="far fa-copy"></i> 复制结果
            </button>
        </div>

        <div class="result">
            <div class="result-header">
                <span class="result-title">转换结果</span>
                <div class="tooltip">
                    <i class="fas fa-info-circle"></i>
                    <span class="tooltiptext">转换后的结果将显示在这里</span>
                </div>
            </div>
            <input type="text" id="snakeCaseOutput" readonly>
        </div>

        <div id="alertMessage" class="alert"></div>
    </div>
    <script src="https://www.unpkg.com/ew-message/dist/ew-message.min.js"></script>
    <script>
        const copyHandler = (str) => {
            const confirm = (content = '已复制到剪切板', type = 'success') => {
                ewMessage[type](content);
            };
            const baseCopy = (copyText) =>
                new Promise((resolve, reject) => {
                    // 判断是否存在clipboard并且是安全的协议
                    if (navigator.clipboard && window.isSecureContext) {
                        navigator.clipboard
                            .writeText(copyText)
                            .then(() => {
                                resolve();
                            })
                            .catch(() => {
                                reject(new Error('复制失败'));
                            });
                    } else {
                        // 否则使用被废弃的execCommand
                        const input = document.createElement('input');
                        input.value = copyText;
                        // 使input不在viewport，同时设置不可见
                        input.style.position = 'absolute';
                        input.style.left = '-9999px';
                        input.style.top = '-9999px';
                        document.body.append(input);
                        input.focus();
                        input.select();
                        // 执行复制命令并移除文本框
                        if (document.execCommand) {
                            document.execCommand('copy');
                            resolve();
                        } else {
                            reject(new Error('复制失败'));
                        }
                        input.remove();
                    }
                });
            baseCopy(str)
                .then(() => confirm())
                .catch(() => confirm('复制失败', 'error'));
        }
        // 驼峰命名转蛇形命名的函数
        const fromCamelCase = (str, separator = '_') =>
            str
                .replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
                .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
                .toLowerCase();

        // 获取HTML元素
        const convertBtn = document.getElementById('convertBtn');
        const copyBtn = document.getElementById('copyBtn');
        const camelCaseInput = document.getElementById('camelCaseInput');
        const camelCaseSymbolInput = document.getElementById('camelCaseSymbolInput');
        const snakeCaseOutput = document.getElementById('snakeCaseOutput');
        const alertMessage = document.getElementById('alertMessage');

        // 点击转换按钮时的事件
        convertBtn.addEventListener('click', () => {
            const camelCaseText = camelCaseInput.value.trim();
            const camelCaseSymbol = camelCaseSymbolInput.value.trim();
            if (camelCaseText === '') {
                alertMessage.innerHTML = '<i class="fas fa-exclamation-circle"></i> 请输入一个有效的驼峰命名字符串！';
                snakeCaseOutput.value = '';
            } else {
                alertMessage.textContent = '';
                // 转换驼峰命名为蛇形命名
                const snakeCaseText = fromCamelCase(camelCaseText, camelCaseSymbol);
                snakeCaseOutput.value = snakeCaseText;
                // 显示成功消息
                alertMessage.innerHTML = '<div class="success-message"><i class="fas fa-check-circle"></i> 转换成功</div>';
                // 3秒后清除成功消息
                setTimeout(() => {
                    alertMessage.textContent = '';
                }, 3000);
            }
        });

        copyBtn.addEventListener('click', () => {
            const value = snakeCaseOutput.value.trim();
            if (value === '') {
                ewMessage.warning("当前没有转换结果，请先转换结果.")
            } else {
                copyHandler(value);
            }
        });

        // 添加键盘事件支持
        camelCaseInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                convertBtn.click();
            }
        });

        camelCaseSymbolInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                convertBtn.click();
            }
        });
    </script>
</body>

</html>